package com.education.wisdom.controller.app;

import com.education.common.core.BaseController;
import com.education.common.pojo.SysUserOther;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.CurrencyRecord;
import com.education.wisdom.pojo.UserAccount;
import com.education.wisdom.pojo.UserResources;
import com.education.wisdom.service.ICurrencyRecordService;
import com.education.wisdom.service.ISysAppLoginService;
import com.education.wisdom.service.IUserAccountService;
import com.education.wisdom.service.IUserResourcesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户上传资源 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-24
 */
@Controller
@Api(tags = {"用户上传资源 信息操作处理"})
@CrossOrigin
@Slf4j
@RequestMapping("/appUserResources")
public class AppUserResourcesController extends BaseController
{
    private String prefix = "system/userResources";
	
	@Autowired
	private IUserResourcesService userResourcesService;
	@Autowired
	private ICurrencyRecordService currencyRecordService;
	@Autowired
	private ISysAppLoginService SysAppLoginService;
	@Autowired
	private IUserAccountService userAccountService;
	@Autowired
	private UserFeign userFeign;
	
	@GetMapping()
	public String userResources()
	{
	    return prefix + "/userResources";
	}

	/**
	 * 根据类别查询资源列表
	 */
	@ApiOperation("根据类别查询资源列表")
	@GetMapping("/typeList")
	@ResponseBody
	@ApiImplicitParam(name = "status", value = "类别", dataType = "String")
	public void typeList(HttpServletResponse response,HttpServletRequest request,String status,String title,Integer pageNo) throws Exception{
		Integer pageSize = 20;
		PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
		log.info(LogUtil.LogInfo("根据类别查询资源列表",new Date()));
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("status", status);
			map.put("title", title);
			List<UserResources> list = userResourcesService.selectByStatus(map);
			writeJSON(new PageInfo<UserResources>(list), response, "200", "成功");
			
	}

	/**
	 * 查询上传资源列表
	 */
	@ApiOperation("查询用户上传资源列表")
	@GetMapping("/pubicList")
	@ApiImplicitParams({
		@ApiImplicitParam(name="uId",value="用户id",required=true,dataType="Integer")
	})
	@ResponseBody
	public void pubicList(HttpServletResponse response,HttpServletRequest request,String uId,String isPubic,Integer pageNo,String title) throws Exception{
		Integer pageSize = 20;
		PageHelper.startPage(pageNo.intValue(), pageSize.intValue());
		log.info(LogUtil.LogInfo("查询用户上传资源列表",new Date()));
		if (uId != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("isPubic", isPubic);
			map.put("uId", uId);
			map.put("title", title);
			List<UserResources> list = userResourcesService.selectPubicList(map);
			writeJSON(new PageInfo<UserResources>(list), response, "200", "成功");
		} 
	}
	
	/**
	 * 保存上传记录
	 */
	@ApiOperation("保存上传记录")
	@PostMapping("/add")
	@ResponseBody
	public void add(HttpServletResponse response,HttpServletRequest request,UserResources userResources) throws Exception{
			log.info(LogUtil.LogInfo("保存上传记录",new Date()));
			String userId = userResources.getUserId();
			UserAccount userAccount = userAccountService.selectUserAccountByUserId(userId);
			Integer currency = userAccount.getCurrency();
			userAccount.setCurrency(currency+1);
			userAccount.setUserId(userId);
			userAccountService.updateUserAccount(userAccount);
			//新增币流水
			CurrencyRecord currencyRecord = new CurrencyRecord();
			currencyRecord.setCurrencyId(UUIDUtils.getByUUId());
			currencyRecord.setCreateTime(new Date());
			currencyRecord.setUserId(userId);
			currencyRecord.setStatus("1");
			currencyRecord.setTrxType("1");
			currencyRecord.setCurrency(new BigDecimal(1));
			SysUserOther sysUserOther = userFeign.selectOnePCF(userId);
			if(sysUserOther != null ){
				String relname = sysUserOther.getSysRelname();
				currencyRecord.setPayerName(relname);
			}
//			SysAppLogin sysAppLogin = SysAppLoginService.selectSysAppLoginById(userId);
//			String relname = sysAppLogin.getUserRelname();
//			currencyRecord.setPayerName(relname);
			currencyRecordService.insertCurrencyRecord(currencyRecord);
			userResources.setCreateTime(new Date());
			userResources.setResourcesId(UUIDUtils.getByUUId());
			//默认通过
			userResources.setIsTure("1");
			writeJSON(userResourcesService.insertUserResources(userResources), response, "200", "成功");
	}


}
